package Food_model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Food_util.DBHelp;




public class Food {
	private String foodid;
	private String foodname;
	private String foodunit;
	private double foodprice;
	
	public String getFoodid() {
		return foodid;
	}
	public void setFoodid(String foodid) {
		this.foodid = foodid;
	}
	public String getFoodname() {
		return foodname;
	}
	public void setFoodname(String foodname) {
		this.foodname = foodname;
	}
	public String getFoodunit() {
		return foodunit;
	}
	public void setFoodunit(String foodunit) {
		this.foodunit = foodunit;
	}
	public double getFoodprice() {
		return foodprice;
	}
	public void setFoodprice(double foodprice) {
		this.foodprice = foodprice;
	}
	
	public static ArrayList<Food>getFoodAll(){
		ArrayList<Food> al=new ArrayList<Food>();
		Connection con=Food_util.DBHelp.getConnection();
		try {
			PreparedStatement st=con.prepareStatement("select * from food");
			ResultSet rs=st.executeQuery();
			while(rs.next()) {
				Food fd=new Food();
				fd.setFoodid(rs.getString("foodid"));
				fd.setFoodname(rs.getString("foodname"));
				fd.setFoodunit(rs.getString("foodunit"));
				fd.setFoodprice(rs.getDouble("foodprice"));
				al.add(fd);
			}
			Food_util.DBHelp.close(rs, st, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return al;
	}
	//根据id进行删除
	public void deleteFoodById() {
		Connection con = DBHelp.getConnection();
		try {
			PreparedStatement st=con.prepareStatement("delete from food where foodid=?");
			st.setString(1, this.foodid);
			st.executeUpdate();
			Food_util.DBHelp.close(null, st, con);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void insertFood() {
		Connection con= Food_util.DBHelp.getConnection();
		try {
			PreparedStatement st = con.prepareStatement("insert into food(foodid,foodname,foodunit,foodprice) values(?,?,?,?)");
			st.setString(1, this.foodid);
			st.setString(2, this.foodname);
			st.setString(3, this.foodunit);
			st.setDouble(4, this.foodprice);
			st.executeUpdate();
			DBHelp.close(null, st, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//根据商品id获取商品信息
	public static 	Food getFoodById(String foodid) {
		Connection con= DBHelp.getConnection();
		Food fd=new Food();
		try {
			PreparedStatement st = con.prepareStatement("select * from food where foodid=?");
			try {
				st.setString(1, foodid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ResultSet rs = st.executeQuery();
			rs.next();
			fd.setFoodid(rs.getString("foodid"));
			fd.setFoodname(rs.getString("foodname"));
			fd.setFoodunit(rs.getString("foodunit"));
			fd.setFoodprice(rs.getDouble("foodprice"));
			DBHelp.close(rs, st, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fd;
	}
	//更新商品信息的方法
	public void updateFood() {
		Connection con= DBHelp.getConnection();
		try {
			PreparedStatement st = con.prepareStatement("update food set foodname=?,foodunit=?,foodprice=? where foodid=?");
			st.setString(1, this.foodname);
			st.setString(2, this.foodunit);
			st.setDouble(3, this.foodprice);
			st.setString(4, this.foodid);
			st.executeUpdate();
			DBHelp.close(null, st, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

	

